package com.allin.social;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.umeng.socialize.Config;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.HashMap;
import java.util.List;

/**
 * TODO<分享弹窗>
 *
 * @author ZhangChangAn
 * @date: 2015年6月29日 下午7:00:21
 */
public class SharePopupWindow extends PopupWindow implements OnClickListener {
    /**
     * 来自个人中心的分享
     */
    public static final int SHARE_FROM_PERSONAL_CENTER = 1;
    /**
     * 来自视频终端的分享
     */
    public static final int SHARE_FROM_TERMINAL_VEDIO = 2;
    /**
     * 来自病例终端的分享
     */
    public static final int SHARE_FROM_TERMINAL_CASE = 3;
    /**
     * 来自话题终端的分享
     */
    public static final int SHARE_FROM_TERMINAL_TOPIC = 4;
    /**
     * 来自会议直播,回播的分享
     */
    public static final int SHARE_FROM_CONFERENCE_LIVE = 5;

    private LinearLayout ll_sina_weibo, ll_weixin, ll_wxcircle, ll_qq, ll_qq_zone, ll_note,ll_email;

    private LayoutInflater inflater;
    private Context context;

    /**
     * 友盟分享
     */
    private SHARE_MEDIA platform;

    private String customer_h5_page_url, nameString, photoPath, hospital, mediclaTitle;
    private String titleString;
    /**
     * 新浪微博的分享内容
     */
    private String sinaContentString;
    /**
     * 短信的分享内容
     */
    private String messageContentString;

    /**
     * 微信和QQ的分享标题
     */
    private String wxAndQQTitleString;
    /**
     * 微信和QQ的分享内容
     */
    private String wxAndQQContentString;

    private OnDismissListener onDismissListener;

    private HashMap umSocialMap;
    private ShareAction shareAction;
    //自定义(友盟默认的有bug,后续修复后去掉)
    private Dialog dialog;

    /**
     * 构造方法
     */
    public SharePopupWindow(Context context, HashMap umSocialMap) {
        this.umSocialMap = umSocialMap;
        this.context = context;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View contentView = inflater.inflate(R.layout.popupwindow_share, null);

        initView(contentView);
        addListener();
        setParams(umSocialMap);
        // 设置分享的内容
        //setShareContent();

        // 设置SelectPicPopupWindow的View
        this.setContentView(contentView);

        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(DensityUtil.dip2px(context, 150));
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);

        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        // this.setAnimationStyle(R.style.popwin_anim_style);
        if (onDismissListener != null) {
            this.setOnDismissListener(onDismissListener);
        }
    }

    private void setParams(HashMap umSocialMap) {
        photoPath = (String) umSocialMap.get("photoPath");
        customer_h5_page_url = (String) umSocialMap.get("customer_h5_page_url");
        sinaContentString = (String) umSocialMap.get("sinaContentString");
        messageContentString = (String) umSocialMap.get("messageContentString");
        wxAndQQTitleString = (String) umSocialMap.get("wxAndQQTitleString");
        wxAndQQContentString = (String) umSocialMap.get("wxAndQQContentString");
    }


    /**
     * 初始化控件
     *
     * @param contentView
     */
    private void initView(View contentView) {
        ll_sina_weibo = (LinearLayout) contentView.findViewById(R.id.ll_sina_weibo);
        ll_weixin = (LinearLayout) contentView.findViewById(R.id.ll_weixin);
        ll_wxcircle = (LinearLayout) contentView.findViewById(R.id.ll_wxcircle);
        ll_qq = (LinearLayout) contentView.findViewById(R.id.ll_qq);
        ll_qq_zone = (LinearLayout) contentView.findViewById(R.id.ll_qq_zone);
        ll_note = (LinearLayout) contentView.findViewById(R.id.ll_note);
        ll_email = (LinearLayout) contentView.findViewById(R.id.ll_email);


        View view = inflater.inflate(R.layout.dialog_cus, null);
        dialog =  new Dialog(context, R.style.progress_dialog);
        dialog.setContentView(view);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        Config.dialog = dialog;

        shareAction = new ShareAction((Activity) context);

    }

    /**
     * 添加监听
     */
    private void addListener() {
        ll_sina_weibo.setOnClickListener(this);
        ll_weixin.setOnClickListener(this);
        ll_wxcircle.setOnClickListener(this);
        ll_qq.setOnClickListener(this);
        ll_qq_zone.setOnClickListener(this);
        ll_note.setOnClickListener(this);
        ll_email.setOnClickListener(this);
    }

    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent, int xoff, int yoff) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent, xoff, yoff);
        } else {
            this.dismiss();
        }
    }

    /**
     * 显示popupwindow在指定位置
     *
     * @param parent
     */
    public void showPopupWindowAtPosition(View parent, int grativity, int x, int y) {
        if (!this.isShowing()) {
            this.showAtLocation(parent, grativity, x, y);
        } else {
            this.dismiss();
        }
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
//			（1-QQ空间，2-QQ好友，3-新浪微博，4-微信好友，5-微信朋友圈 ，6短信
            case R.id.ll_sina_weibo:
                platform = SHARE_MEDIA.SINA;
                umSocialMap.put("shareChannel", "3");

                shareAction.setPlatform(platform)
                        .setCallback(umShareListener)
                        .withText(sinaContentString)
                        .withMedia(new UMImage(context, photoPath))
                        .share();

                break;
            case R.id.ll_weixin:
                platform = SHARE_MEDIA.WEIXIN;
                umSocialMap.put("shareChannel", "4");

                shareAction.setPlatform(platform)
                        .setCallback(umShareListener)
                        .withTitle(wxAndQQTitleString)
                        .withText("微信分享")
                        .withTargetUrl(customer_h5_page_url)
                        .withMedia(new UMImage(context, photoPath))
                        .share();

                break;
            case R.id.ll_wxcircle:
                platform = SHARE_MEDIA.WEIXIN_CIRCLE;

                umSocialMap.put("shareChannel", "5");

                shareAction.setPlatform(platform)
                        .setCallback(umShareListener)
                        .withTitle(wxAndQQTitleString)
                        .withText("微信分享")
                        .withTargetUrl(customer_h5_page_url)
                        .withMedia(new UMImage(context, photoPath))
                        .share();


                break;
            case R.id.ll_qq:
                platform = SHARE_MEDIA.QQ;
                umSocialMap.put("shareChannel", "2");

                shareAction.setPlatform(platform)
                        .setCallback(umShareListener)
                        .withTitle(wxAndQQTitleString)
                        .withText("QQ分享")
                        .withTargetUrl(customer_h5_page_url)
                        .withMedia(new UMImage(context, photoPath))
                        .share();

                break;
            case R.id.ll_qq_zone:
                platform = SHARE_MEDIA.QZONE;
                umSocialMap.put("shareChannel", "1");
                shareAction.setPlatform(platform)
                        .setCallback(umShareListener)
                        .withTitle(wxAndQQTitleString)
                        .withText("QQ空间分享")
                        .withTargetUrl(customer_h5_page_url)
                        .withMedia(new UMImage(context, photoPath))
                        .share();

                break;
            case R.id.ll_note:// 短信
                platform = SHARE_MEDIA.SMS;
                umSocialMap.put("shareChannel", "6");

                shareAction.setPlatform(platform)
                        .setCallback(umShareListener)
                        .withTitle("短信分享")
                        .withText(messageContentString)
                        .withTargetUrl(customer_h5_page_url)
                        .withMedia(new UMImage(context, photoPath))
                        .share();
                break;
            case R.id.ll_email:// 邮件
                platform = SHARE_MEDIA.EMAIL;
                umSocialMap.put("shareChannel", "6");

                shareAction.setPlatform(platform)
                        .setCallback(umShareListener)
                        .withTitle("邮件分享")
                        .withText(messageContentString)
                        .share();
//                getEmail();
                break;
            default:
                break;
        }
        this.dismiss();
    }

    /**
     * 分享监听器
     */
    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(context, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(context, platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(context, platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };


    private void getEmail() {
        Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setType("text/html");
        List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(intent, 0);

        if (!resInfo.isEmpty()) {
            for (ResolveInfo info : resInfo) {
                if (info.activityInfo.packageName.toLowerCase().contains("email") || info.activityInfo.name.toLowerCase().contains("email")) {
                    intent.putExtra(android.content.Intent.EXTRA_TEXT, "网页内容是啥活动的");
                    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "分享邮件的标题");

                    intent.setPackage(info.activityInfo.packageName);
                    context.startActivity(Intent.createChooser(intent, "分享标题"));
                }else {
                    Toast.makeText(context, "抱歉,您手机上没有可用的邮件客户端!", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
